package com.rasenhund.android.grecorder.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.rasenhund.android.grecorder.R;
import com.rasenhund.android.grecorder.common.Const;
import com.rasenhund.android.grecorder.schema.StageRecord;

/**
 * 戦闘履歴 ListAdapter
 */
public class HistoryListAdapter extends ArrayAdapter<StageRecord> {
    
    private Context mContext;

    private List<StageRecord> items;
    private LayoutInflater mInflater;
    
    public HistoryListAdapter(Context context, List<StageRecord> objects) {
        super(context, 0, objects);
        
        mContext = context;

        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        items = objects;
        
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        View view;

        if (convertView != null) {
            view = convertView;
        } else {
            view = mInflater.inflate(R.layout.list_history_row, null);
        }

        final StageRecord item = (StageRecord) getItem(position);

        if (item != null) { 
            
            TextView stageNm = (TextView) view.findViewById(R.id.stageNm);
            String sStageNm = Const.getStageNm(mContext, item.getStageCd());
            stageNm.setText(sStageNm);
            
            TextView categoryNm = (TextView) view.findViewById(R.id.categoryNm);
            String sCategoryNm = Const.getCategoryNm(mContext, item.getCategoryCd());
            categoryNm.setText(sCategoryNm);
            
            TextView result = (TextView) view.findViewById(R.id.result);
            String sResultNm = Const.getResultNm(mContext, item.getResultCd());
            result.setText(sResultNm);
            
            TextView insertDate = (TextView) view.findViewById(R.id.insertDate);
            String sInsertDate = item.getInsertDate();
            insertDate.setText(sInsertDate);

            // 戦闘結果により背景styleを切り替える
            switch (item.getResultCd()) {
                case Const.RESULT.WIN:
                case Const.RESULT.PERFECT_WIN:
                    view.setBackgroundResource(R.drawable.rowstyle_history_win);
                    break;
                case Const.RESULT.LOSE:
                case Const.RESULT.PERFECT_LOSE:
                    view.setBackgroundResource(R.drawable.rowstyle_history_lose);
                    break;
                default:
                    view.setBackgroundResource(R.drawable.rowstyle_history_draw);
                    break;
            }

            // ImageButton btnEdit = (ImageButton)
            // view.findViewById(R.id.btnEdit);
            // btnEdit.setOnClickListener(new View.OnClickListener() {
            //
            // @Override
            // public void onClick(View v) {
            //
            // Intent intent = new Intent(mContext,
            // HistoryDetailActivity.class);
            // intent.putExtra(Const.KEY.KEY_RECORD_ITEM, item);
            // mContext.startActivity(intent);
            //
            // }
            // });

        }

        return view;

    }
    
    @Override
    public StageRecord getItem(int position) {
        return items.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return items.get(position).getRowId();
    }

    /********************
     * EventListener
     ********************/

    // /**
    // * 編集ボタン押下時のイベントリスナ
    // */
    // private final OnClickListener btnEdit_OnClickListener = new
    // OnClickListener() {
    // @Override
    // public void onClick(DialogInterface dialog, int which) {
    //
    // }
    // };

    /********************
     * Method
     ********************/
    /**
     * 任意の項目の削除
     * 
     * @param position
     */
    public void removeAt(int position) {
        this.items.remove(position);
    }
        
}
